package com.chis.jmdatatimer.parasecode;

import com.chis.jm.bean.qczs.data.ElecCode;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * 
 * @Description 
 * @Company zwx
 * @author wlj
 * @version 2019年01月21日
 */
public class Test {

    public static List<String> list = null;
    public static void main(String[] args) {
        list = new ArrayList<>();
        list.add("201806032");
        list.add("201803014");
        list.add("201612112");
        list.add("201703042");
        list.add("201801010");
        list.add("201703049");
        list.add("201704053");
        list.add("201709023");
        list.add("201704065");
        list.add("201702031");
        list.add("201803004");
        list.add("201708028");
        list.add("201709032");
        list.add("201801005");
        list.add("201801002");
        list.add("201804018");
        list.add("201712170");
        list.add("201705031-2");
        list.add("201702007-3");
        list.add("201707050-1");
        list.add("201802005-1");
        list.add("201708008-2");
        list.add("201801006-2");
        list.add("201803023-1");
        list.add("201803024-1");
        list.add("201707028");
        list.add("20180406");
        list.add("201804A069-2");
        list.add("201802A026-2");
        list.add("201804A086-1");
        list.add("201704a040");
        list.add("201707a057");
        list.add("201703a027");
        list.add("201603a029");
        list.add("201804035T");
        list.add("201804035T");
        list.add("201803025T");
        list.add("201803024-1");
        list.add("20180721");
        list.add("201805038");
        list.add("201706082-1");
        list.add("201611XB08");
        list.add("201611XB08");
        list.add("B201605021");
        list.add("A201606001");
        list.add("A201606002");
        list.add("C201706068");
        list.add("C201703024");
        list.add("C201612156");
        list.add("AHPVA344AA");
        list.add("N3N121M");
        list.add("P3K101M");
        list.add("N3N611M");
        list.add("A201706008");
        list.add("G201707005");
        list.add("G201708011");
        list.add("D201801001");
        list.add("D201709096");
        list.add("201810B055");
        list.add("201705037-2");
        list.add("201701005-2");
        list.add("201612159-2");
        list.add("201706039-2");
        list.add("A20171105");
        list.add("A20180102");
        list.add("20161151-2");
        list.add("20171136-1");
        list.add("201801007");
        list.add("201703004");
        list.add("20170708");
        list.add("20170809");
        list.add("20171118");
        list.add("20180809");
        list.add("20180911");
        traverseFolder2("C:\\Users\\Administrator\\Desktop\\mycode");
    }

    public static void traverseFolder2(String path) {

        File file = new File(path);
        if (file.exists()) {
            File[] files = file.listFiles();
            if (null == files || files.length == 0) {
                return;
            } else {
                for (File file2 : files) {
                    if (file2.isDirectory()) {
                        //System.out.println("文件夹:" + file2.getAbsolutePath());
                        traverseFolder2(file2.getAbsolutePath());
                    } else {
                        if(file2.getAbsolutePath().endsWith(".xml")){
//                            System.out.println("文件路径:" + file2.getAbsolutePath());
                            parse(file2);
//                            System.out.println("===================================");

                        }
                    }
                }
            }
        } else {
            System.out.println("文件不存在!");
        }
    }

    public  static void parse(File file2) {

        SAXReader reader = new SAXReader();
        try {
            Document doc = reader.read(file2);
            Element root = doc.getRootElement();
            Iterator it = root.elementIterator();
            while (it.hasNext()) {
                Element events = (Element) it.next();
                List<Element> eventList = events.elements();
                for (Element event : eventList) {
                    List<Element> relationList = event.elements();
                    for (Element relation : relationList) {
                        //
                        List<Attribute> relationAttrs = relation.attributes();
                        //电子监管码
                        List<Element> batchList = relation.elements();
                        for (Element batch : batchList) {
                            List<Attribute> batchAttrs = batch.attributes();
                            for (Attribute batchAttr : batchAttrs){
                                if(batchAttr.getName().equals("batchNo")){
                                    String bat = batchAttr.getText();
                                    if(list.contains(bat)){
                                        System.out.println("批号："+bat);
                                        System.out.println("文件路径：" + file2.getAbsolutePath());
                                    }
                                }
                                //System.out.println("属性："+batchAttr.getName()+"内容："+batchAttr.getText());
                            }
//                            List<Element> codeList = batch.elements();
//                            for (Element code : codeList) {
//                                List<Attribute> codeAttrs = code.attributes();
//                                for (Attribute codeAttr : codeAttrs){
//                                    System.out.println(codeAttr.getName()+codeAttr.getText());
//                                }
//                            }
                        }
                    }
                }
            }

        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }



}
